
//
//  LandingViewController.m
//  UI14_豆瓣完整版
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LandingViewController.h"
#import "LoginViewController.h"
#import "BYJDataBaseHandle.h"
#import "UserModel.h"

@interface LandingViewController ()

@property (nonatomic, retain) UITextField *nameTF;
@property (nonatomic, retain) UITextField *codeTF;


@end

@implementation LandingViewController

- (void)dealloc {
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 50)];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.text = @"用户名：";
    [self.view addSubview:nameLabel];
    [nameLabel release];
    
    UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 100, 50)];
    codeLabel.backgroundColor = [UIColor clearColor];
    codeLabel.text = @"密码：";
    [self.view addSubview:codeLabel];
    [codeLabel release];
    
    self.nameTF
    = [[UITextField alloc] initWithFrame:CGRectMake(130, 20, 200, 50)];
    _nameTF.backgroundColor = [UIColor whiteColor];
    _nameTF.layer.borderWidth = 2;
    _nameTF.placeholder = @"请输入用户名";
    [self.view addSubview:_nameTF];
    [_nameTF release];
    
    self.codeTF = [[UITextField alloc] initWithFrame:CGRectMake(130, 80, 200, 50)];
    _codeTF.backgroundColor = [UIColor clearColor];
    _codeTF.layer.borderWidth = 2;
    _codeTF.placeholder = @"请输入密码";
    [self.view addSubview:_codeTF];
    [_codeTF release];
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];
    button1.backgroundColor = [UIColor magentaColor];
    [button1 setTitle:@"登陆" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(220, 150, 100, 50)];
    button2.backgroundColor = [UIColor cyanColor];
    [button2 setTitle:@"注册" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIBarButtonItem *item =[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item;
}

- (void)itemAction:(UIBarButtonItem *)item {
    
    [self.navigationController popToRootViewControllerAnimated:YES];

}

- (void)buttonAction1:(UIButton *)button {
    
    // 从数据库中查找
    NSMutableArray *arr = [[BYJDataBaseHandle sharedDataBaseHandle] selectUserTable];
    NSMutableArray *modelArr = [NSMutableArray array];
    for (UserModel *model in arr) {
        _nameTF.text = model.name;
        _codeTF.text = model.code;
        [modelArr addObject:model];
    }
    if (![modelArr containsObject:_nameTF.text] &&
        ![modelArr containsObject:_codeTF.text]) {
        
        UserModel *modle = [UserModel userModel];
        modle.name = _nameTF.text;
        modle.code = _codeTF.text;
        
        [[BYJDataBaseHandle sharedDataBaseHandle] insertUserModel:modle];
    }
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
#warning 6.让代理人执行方法
    [self.delegate returnTextWithString:@"注销"];
}

- (void)buttonAction2:(UIButton *)button {
    
    LoginViewController *logVc = [[LoginViewController alloc] init];
    [self presentViewController:logVc animated:YES completion:nil];
    [logVc release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
